package com.drink.yan.domain;

import com.drink.yan.intf.define.YanOrderStatusE;
import com.drink.yan.intf.entity.YanOrder;
import com.drink.yan.intf.entity.YanOrderItem;
import com.drink.yan.intf.entity.YanSku;
import com.drink.yan.intf.vo.YanUserDetailVo;
import com.drink.yan.web.rest.req.cmd.ApplyOrderCmd;
import com.drink.yan.web.rest.req.cmd.OrderOperateCmd;
import com.github.meixuesong.aggregatepersistence.Aggregate;
import com.github.meixuesong.aggregatepersistence.AggregateFactory;
import com.github.meixuesong.aggregatepersistence.Versionable;
import com.meta.framework.common.utils.SecurityUtils;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * java类简单作用描述
 *
 * @Title:
 * @Package: com.drink.yan.domain.
 * @Author: M.simple
 * @Remark: The modified content
 * @CreateDate: 2023-12-09 13:42
 * @Version: v2.0
 */
@Data
public class YanOrderDo extends YanOrder implements Versionable {


	private List<YanOrderItem> yanOrderItems;


	public static Aggregate<YanOrderDo> createOrder(ApplyOrderCmd cmd, List<YanSku> yanSkus, YanUserDo root) {

		if (CollectionUtils.isEmpty(yanSkus)) {
			return null;
		}
		List<String> skuNameList = yanSkus.stream().map(YanSku::getSkuName).collect(Collectors.toList());
		YanOrderDo yanOrderDo = new YanOrderDo();
		yanOrderDo.setOrderType(cmd.getOrderType());
		yanOrderDo.setOrderPrice(cmd.getOrderPrice());
		yanOrderDo.setOrderName(String.join("|", skuNameList));
		yanOrderDo.setCustomerUid(cmd.getCustomerUid());
		yanOrderDo.setExpressType(cmd.getExpressType());
		yanOrderDo.setOrderPrice(cmd.getOrderPrice());
		yanOrderDo.setOrderStatus(YanOrderStatusE.created.getCode());
		yanOrderDo.setOrderAddress(cmd.getOrderAddress());
		yanOrderDo.setOpSource(cmd.getOpSource());
		yanOrderDo.setCreateBy(SecurityUtils.getUserId());
		YanUserDetailVo yanUserDetail = root.getYanUserDetail();
		if (Objects.nonNull(yanUserDetail)) {
			yanOrderDo.setCustomerName(yanUserDetail.getUserName());
			yanOrderDo.setCustomerTel(yanUserDetail.getMobile());
		}

		List<YanOrderItem> orderItems = createOrderItems(yanSkus);
		yanOrderDo.setYanOrderItems(orderItems);

		return AggregateFactory.createAggregate(yanOrderDo);
	}

	private static List<YanOrderItem> createOrderItems(List<YanSku> yanSkus) {

		List<YanOrderItem> objects = new ArrayList<>();
		for (YanSku sku: yanSkus){
			YanOrderItem yanOrderItem = new YanOrderItem();
			yanOrderItem.setOrderNo(null);
			yanOrderItem.setSkuId(sku.getSkuId());
			yanOrderItem.setSkuName(sku.getSkuName());
			yanOrderItem.setCreateBy(SecurityUtils.getUserId());

			objects.add(yanOrderItem);
		}

		return objects;
	}

	@Override
	public int getVersion() {
		return 0;
	}

	public void payOrder(OrderOperateCmd cmd) {
		this.setOrderStatus(YanOrderStatusE.paid.getCode());
	}

	public void deliverOrder(OrderOperateCmd cmd) {
		this.setOrderStatus(YanOrderStatusE.delivered.getCode());

	}

	public void receiveOrder(OrderOperateCmd cmd) {
		this.setOrderStatus(YanOrderStatusE.received.getCode());

	}

	public void completeOrder(OrderOperateCmd cmd) {
		this.setOrderStatus(YanOrderStatusE.completed.getCode());

	}

	public void cancelOrder(OrderOperateCmd cmd) {
		this.setOrderStatus(YanOrderStatusE.cancelled.getCode());
		this.setRemark(cmd.getRemark());
	}
}
